import {
	uploadFill,
	axios
} from '../server/index.js'
import router from '@/router/index.js'
import Vue from 'vue'
import store from '@/store/index.js'
const vm = new Vue()


// 将数据格式化成上传文件格式 
export const objToFormData = (obj) => {
	let formdata = new FormData()
	for (let k in obj) {
		if (obj[k] instanceof Array) {
			for (let index in obj[k]) {
				formdata.append(`${k}[$P{index}]`, obj[k][index])
			}
		} else {
			formdata.append(k, obj[k])
		}
	}
	return formdata
}

// 格式化谷歌登录数据
export const initGoogleData = (googleUser) => {
	const info = googleUser.getBasicProfile() //获取信息
	const nickname = info.getName(); // 姓名
	const googleOpenId = info.getId(); // id
	const givenName = info.getGivenName(); // 名
	const familyName = info.getFamilyName(); // 姓
	const imageUrl = info.getImageUrl(); // imageUrl
	const email = info.getEmail(); // email
	return {
		clickId: null,
		email,
		googleOpenId,
		nickname,
		sourceUserId: window.localStorage.getItem('sourceUserId'),
		subChannelId: null
	}
}
// 获取FaceBook用户信息
export const initFaceBookData = (data) => {
	return new Promise((rev, rej) => {
		if (data.status != "connected") {
			rej()
		}
		axios({
			url: 'https://graph.facebook.com/v6.0/me',
			params: {
				access_token: data.authResponse.accessToken,
				fields: ' email,name',
				method: 'get',
				pretty: 0,
				scope: 'email',
				sdk: 'joey',
				suppress_http_code: 1
			}
		}).then(res => {
			rev({
				email: res.email,
				facebookOpenId: res.id,
				nickname: res.name,
				sourceUserId: window.localStorage.getItem('sourceUserId'),
			})
		}).catch(err => {
			rej(err)
		})
	})
}


// 判断用户45天内是否已经购买过该产品
export const JudgeUserHasPurchased = (reviewTaskId) => {
	// reviewTaskId = 486
	console.log(reviewTaskId)
	vm.$loading()
	return new Promise((rev, rej) => {
		axios({
			url: 'user/order/appSiteOrders?current=1&size=30&source=3&status=11,12'
		}).then(res => {
			let list = res.data.records
			res.data.records.forEach(item => {
				if (reviewTaskId == item.reviewTaskId) {
					if (((new Date().valueOf()) - (new Date(item.orderTime).valueOf())) <
						3888000000) {
						// 代表间隔时间小于45天 禁止用户下单
						vm.showToast("You have purchased the product within 45 days",
							'45日間でこの商品を購入しました。')
						console.log("间隔时间小于45天 禁止用户下单")
						rej()
					}
				}
			})
			rev()
		})
	})
}

// 上传一张图片
export const uploadOneImg = (url) => {
	// name为file
	return new Promise((resolve, reject) => {
		uploadFill({
			url: 'index/media/init?type=2',
			method: 'post',
			data: objToFormData({
				file: url
			})
		}).then(res => {
			resolve(res.data)
		}).catch(err => {
			reject(err)
		})
	})
}


// 千分位分割
export const statistic = (num) => {
	let [integer, decimal] = String.prototype.split.call(num, '.')
	integer = (integer || 0).toString()
	let result = ''
	while (integer.length > 3) {
		result = ',' + integer.slice(-3) + result
		integer = integer.slice(0, integer.length - 3)
	}
	if (integer) {
		result = integer + result
	}
	return `${result}${decimal ? '.' + decimal : ''}`
}

// 亚马逊订单号正则
export const AmountOrderTest = (val) => {
	return /^[0-9a-zA-Z]{3}-[0-9a-zA-Z]{7}-[0-9a-zA-Z]{7}/.test(val)
}
// 邮箱正则
export const emailTest = (val) => {
	return /^\w+(?:\.\w+){0,1}@[a-zA-Z0-9]{2,14}(?:\.[a-z]{2,4}){1,2}$/.test(val)
}

// 判断是否登录 没登录跳转到登录页
export const isJudgmentLoading = (val) => {
	if (!store.state.isLogin) {
		router.push('/login')
		return false
	}
	return true
}
